# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: tb.py
@time: 2021/9/28 23:29
'''
import datetime
import random
import time
from selenium import webdriver
from icecream import ic
import pymongo
import pymysql

url = 'https://s.taobao.com/search?q=%E4%B9%90%E9%AB%98%E7%A7%AF%E6%9C%A8&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20210929&ie=utf8'

options = webdriver.ChromeOptions()
user_ag='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
options.add_argument('user-agent=%s'%user_ag)
options.add_argument('disable-infobars')
options.add_argument('--disable-blink-features=AutomationControlled')
browser = webdriver.Chrome(chrome_options = options)
browser.execute_script("Object.defineProperties(navigator,{webdriver:{get:() => false}})") #淘宝对selenium的识别主要是通过navigator.webdriver,使用selenium的浏览器api显示的是True，所有我们改成fALSE就可以过淘宝的检测
#登录 扫码登录
browser.get(url)
time.sleep(2)
# browser.find_element_by_class_name('alipay-login').click()
time.sleep(20)

count = 0
browser.get(url)
time.sleep(3)
def tb():
    title = browser.find_elements_by_xpath('//div[@class="row row-2 title"]/a')
    price = browser.find_elements_by_xpath('//div[@class="ctx-box J_MouseEneterLeave J_IconMoreNew"]/div/div/strong')
    merchant = browser.find_elements_by_xpath('//div[@class="row row-3 g-clearfix"]/div/a/span[2]')
    location = browser.find_elements_by_xpath('//div[@class="row row-3 g-clearfix"]/div[@class="location"]')
    pay_count = browser.find_elements_by_xpath('//div[@class="row row-1 g-clearfix"]/div[@class="deal-cnt"]')

    db = pymysql.connect(host ='127.0.0.1', database = 'taobao',user = 'root',password = '123456')
    cursor = db.cursor()
    for i in range (0,len(title)):
        ic(title[i].text)
        sql = 'insert into tb (title,price,merchant,location,pay_count) values (%s,%s,%s,%s,%s)'
        cursor.execute(sql,(
            title[i].text,
            price[i].text,
            merchant[i].text,
            location[i].text,
            pay_count[i].text
        ))
        db.commit()
    global count
    count +=  1
    ic(f'第{count}页完成')

    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    try:
        next = browser.find_element_by_xpath('//span[@class="icon icon-btn-next-2"]')
        if next == None:
            browser.find_element_by_xpath('//span[@class="icon icon-btn-next-2"]').click()
            ic('下一页===========>')
            time.sleep(random.randint(4,7))
            tb()
        else:
            ic('爬完了')
    except Exception as e:
        ic('爬完了====>%s'%e)

start = time.time()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
tb()
print(time.time()-start)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))










# ic(browser.get_cookies())
# time.sleep(3)
# browser.delete_all_cookies()
# for k in list_cookie:
#     # ic(k)
#     browser.add_cookie(k)
# ic(browser.get_cookies())
# time.sleep(2)
# browser.get('http://s.taobao.com/search?initiative_id=tbindexz_20170306&ie=utf8&spm=a21bo.21814703.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E4%B9%90%E9%AB%98%E7%A7%AF%E6%9C%A8&suggest=0_2&_input_charset=utf-8&wq=lega&suggest_query=lega&source=suggest')
# time.sleep(20)


